package edu.xhu.minio.config;

import edu.xhu.minio.clients.BiliMinIOClient;
import edu.xhu.minio.clients.BiliSSHCilent;
import edu.xhu.model.minio.constant.MinioConstants;
import io.minio.MinioClient;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.Map;

@Data
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties({BiliMinIOConfigProperties.class})
public class BiliMinIOConfig {

    private final BiliMinIOConfigProperties biliMinIOConfigProperties;

    private final RedisTemplate redisTemplate;

    /**
     * @return 多个自定义BiliMinIOClient，biliMinIOClientMap={(ip:BiliMinIOClient),(ip:BiliMinIOClient)}
     */
    @Bean
    public Map<String, BiliMinIOClient> buildBiliMinIOClientMap() {
        //Map<ip地址，自定义minio客户端>
        Map<String, BiliMinIOClient> biliMinIOClientMap = new HashMap<>();

        int endpointsSize = biliMinIOConfigProperties.getEndpoints().size();
        int hostsSize = biliMinIOConfigProperties.getHosts().size();
        int usersSize = biliMinIOConfigProperties.getUsers().size();
        int passwdsSize = biliMinIOConfigProperties.getPasswds().size();

        int minSize = Math.min(Math.min(endpointsSize, hostsSize), Math.min(usersSize, passwdsSize));

        for (int i = 0; i < minSize; i++) {
            BiliMinIOClient biliMinIOClient = new BiliMinIOClient();

            //设置minio客户端
            MinioClient minioClient = MinioClient
                    .builder()
                    .credentials(biliMinIOConfigProperties.getAccessKey(), biliMinIOConfigProperties.getSecretKey())
                    .endpoint(biliMinIOConfigProperties.getEndpoints().get(i))
                    .build();

            //配置自定义SSH客户端
            BiliSSHCilent biliSSHCilent = new BiliSSHCilent();
            biliSSHCilent.setHost(biliMinIOConfigProperties.getHosts().get(i))
                    .setUser(biliMinIOConfigProperties.getUsers().get(i))
                    .setPasswd(biliMinIOConfigProperties.getPasswds().get(i))
                    .connect(); //提前连接好远程服务器

            //打包
            biliMinIOClient
                    .setMinioClient(minioClient)
                    .setBiliSSHCilent(biliSSHCilent)
                    .setReadRootPath(biliMinIOConfigProperties.getEndpoints().get(i));

            biliMinIOClientMap.put(biliMinIOConfigProperties.getHosts().get(i), biliMinIOClient);
        }


        // 提前设置好最小连接负载均衡 zset
        // 判断相应 zset 是否存在
        if (!redisTemplate.hasKey(MinioConstants.MIN_LOAD_BALANBER_ZSET)) {
            for (int i = 0; i < minSize; i++) {
                String host = biliMinIOConfigProperties.getHosts().get(i);
                redisTemplate.opsForZSet().add(MinioConstants.MIN_LOAD_BALANBER_ZSET, host, 0);
            }
        }

        return biliMinIOClientMap;
    }

}
